package com.c.ctgproject.ui.storage.car

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.storage.StorageListAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.storage.StorageListEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_car_info_list.*
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2020/10/19
 *描述:仓储 库位列表
 */
class StorageListActivity : BaseActivity(), StorageListAdapter.StorageCallback {
    private val mViewModel: StorageListViewModel by lazy { ViewModelProvider(this)[StorageListViewModel::class.java] }
    private val mList = ArrayList<StorageListEntity.DataBean.ListBean>()
    private val mAdapter: StorageListAdapter by lazy { StorageListAdapter(this, mList, this) }

    private var page = 1
    private var size = 20


    override fun event() {
        changeTransparentStatusBar()
        mStorageRV.adapter = mAdapter
        mStorageRV.layoutManager = LinearLayoutManager(this)
        getData()
        mViewModel.error.observe(this, Observer {
            mRefresh.over()
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })
        mViewModel.result.observe(this, Observer {
            mRefresh.over()
            loading.dismiss()
            MyLog.d("库位列表:$it")
            val data = initData(it)
            if (data.status == 0) {
                val entity = getGson().fromJson(it, StorageListEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                showToast(data.msg)
            }
        })
    }

    private fun getData() {
        loading.show()
        mViewModel.getStorageList(page, size)
    }
    override fun click() {
        mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }
        mRefresh.setOnRefreshListener {
            mList.clear()
            page=1
            getData()
        }
    }

    override fun getLayout() = R.layout.activity_car_info_list

    override fun onItemClick(id: Int) {
        startActivity<StorageCarListActivity>(MyConstants.STORAGE_ID to id)
    }
}